package com.example.k.littletag.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


import com.example.k.littletag.utils.Note;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by K on 2015/8/16.
 */
public class NoteDB {
    public static final String DB_NAME = "note";
    public static final int VERSION = 1;
    private SQLiteDatabase sqLiteDatabase;
    private static NoteDB noteDB;

    private NoteDB(Context context) {
        NoteOpenHelper noteOpenHelper = new NoteOpenHelper(context, DB_NAME, null, VERSION);
        sqLiteDatabase = noteOpenHelper.getWritableDatabase();
    }

    public synchronized static NoteDB getNoteDB(Context context) {
        if (noteDB == null) {
            noteDB = new NoteDB(context);
        }
        return noteDB;
    }

    /**
     * 存储日记内容
     */

    public void saveNote(Note note) {
        if (note != null) {
            ContentValues values = new ContentValues();
            //values.put("id", note.getId());
            int[] time = note.getTime();
            values.put("time_year", time[0]);
            values.put("time_month", time[1]);
            values.put("time_day", time[2]);
            values.put("time_hour", time[3]);
            values.put("time_minute", time[4]);
            values.put("content", note.getContent());
            values.put("checkCount", note.getCheckCount());
            values.put("isFinish", note.getIsFinish());
            sqLiteDatabase.insert("Notes", null, values);
        }
    }

    /**
     * 读取日记内容
     */

    public List<Note> loadNote() {
        List<Note> list = new ArrayList<>();
        Cursor cursor = sqLiteDatabase.query("Notes", null, null, null, null, null, null);
        if (cursor.moveToFirst()) {
                do {
                    if (cursor.getInt(cursor.getColumnIndex("isFinish"))==0) {
                        Note note = new Note();
                        int[] time = {cursor.getInt(cursor.getColumnIndex("time_year")),
                                cursor.getInt(cursor.getColumnIndex("time_month")),
                                cursor.getInt(cursor.getColumnIndex("time_day")),
                                cursor.getInt(cursor.getColumnIndex("time_hour")),
                                cursor.getInt(cursor.getColumnIndex("time_minute")),
                                0};
                        note.setTime(time);
                        note.setContent(cursor.getString(cursor.getColumnIndex("content")));
                        note.setCheckCount(cursor.getInt(cursor.getColumnIndex("checkCount")));
                        note.setIsFinish(cursor.getInt(cursor.getColumnIndex("isFinish")));
                        list.add(note);
                    }
                } while (cursor.moveToNext());
            }

        if (cursor.moveToFirst()) {
            do {
                if (cursor.getInt(cursor.getColumnIndex("isFinish"))==1) {
                    Note note = new Note();
                    int[] time = {cursor.getInt(cursor.getColumnIndex("time_year")),
                            cursor.getInt(cursor.getColumnIndex("time_month")),
                            cursor.getInt(cursor.getColumnIndex("time_day")),
                            cursor.getInt(cursor.getColumnIndex("time_hour")),
                            cursor.getInt(cursor.getColumnIndex("time_minute")),
                            0};
                    note.setTime(time);
                    note.setContent(cursor.getString(cursor.getColumnIndex("content")));
                    note.setCheckCount(cursor.getInt(cursor.getColumnIndex("checkCount")));
                    note.setIsFinish(cursor.getInt(cursor.getColumnIndex("isFinish")));
                    list.add(note);
                }
            } while (cursor.moveToNext());
        }

        return list;
    }

    public void deleteNote(Note note) {
        String[] content = {note.getContent()};
        sqLiteDatabase.delete("Notes", "content=?", content);
    }

}
